//
//  WBNetApi.swift
//  微博
//
//  Created by admin on 2018/3/22.
//  Copyright © 2018年 xm. All rights reserved.
//

import Foundation

extension NetworkManager{
    
    
    /// 获取首页微博列表
    ///
    /// - Parameters:
    ///   - sinceId: 开始id
    ///   - maxId:
    ///   - completion:  完成回调
    func getStatusList(sinceId:Int64,maxId: Int64,completion:@escaping ([String:AnyObject]?,Bool)->()) -> () {
        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
        //请求参数
        //swift中 Int可以转换成 AnyObject但是Int64不行
        let params = ["sinceId":"\(sinceId)",
                      "maxId":"\(maxId)"] //swift中 Int可以转换成 AnyObject但是Int64不行
        requestWithToken(URLString: urlString, parameters: params) { (json,isSuccess) in
            if (isSuccess){
                 completion(json as! [String : AnyObject],isSuccess)//回调闭包
            }else{
                 completion(nil,isSuccess)//回调闭包
            }
            print(json)
        }
    }
    
    
    /// 检查新微博未读数量
    ///
    /// - Parameters:
    ///   - uid: <#uid description#>
    ///   - completion: <#completion description#>
    func unreadCount(completion:@escaping (Int)->()) -> () {
        let urlString = "https://api.weibo.com/2/remind/unread_count.json"
        let params=["uid" : userAccount.uid]
        requestWithToken(URLString: urlString, parameters: params) { (json,isSuccess) in
            if (isSuccess){
                let dict = json as? [String: AnyObject]
                let count = dict?["status"] as! Int 
                completion(count)//回调闭包
            }else{
                completion(0)//回调闭包
            }
        } 
    }
    
    /// 获取token信息
    ///
    /// - Parameters:
    ///   - code: 请求需要的授权code
    ///   - completion: 回调
    func getAccessToken(code:String,completion:@escaping (Bool)->()) -> () {
        let urlString = "https://api.weibo.com/oauth2/access_token"
        //请求参数
        let params = ["client_id": WBAPPKEY,
                      "client_secret": WBAPPSECRET,
                      "grant_type": "authorization_code",
                      "code": code,
                      "redirect_uri": WBREDIRECT_URI
        ] 
        request(method:.POST, URLString: urlString, parameters: params) { (json,isSuccess) in
            if (isSuccess){
                self.userAccount.yy_modelSet(with: (json as? [String: AnyObject]) ?? [:])
                self.getUserInfo(completion: { (dict, isSuccess) in
                    if isSuccess{
                        self.userAccount.yy_modelSet(with: dict ?? [:])
                        self.userAccount.saveAccount()//保存账户模型数据到本地
                        completion(true)//回调闭包
                    }else{
                        completion(false)//回调闭包
                    }
                }) 
            }else{
                completion(false)//回调闭包
            }
        }
    }
    
    /// 查询用户信息
    ///
    /// - Parameter completion: 完成回调
    func getUserInfo(completion:@escaping ([String:AnyObject]?,Bool)->()) -> () {
        // 请求地址
        let urlString = "https://api.weibo.com/2/users/show.json"
        // 请求参数
        let params = ["uid": userAccount.uid]
        requestWithToken(URLString: urlString, parameters: params) { (json,isSuccess) in
            if (isSuccess){
                completion(json as! [String : AnyObject],isSuccess)//回调闭包
            }else{
                completion(nil,isSuccess)//回调闭包
            }
        }
    }
    
    
}
